package com.thyc.server;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.thyc.entity.SysConfig;
import com.thyc.entity.WarningContacts;
import com.thyc.entity.WarningInfo;
import com.thyc.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

//发送短信  告知设备监测信息异常
@Component
@Slf4j
public class SendInfo {

    private static final String AccessKeyId = "LTAIh5198YIAeHTj";
    private static final String AccessKeySecret = "BX9sWgenS5znmuVyGEqzlCchGhBRPd";
    private static final String SignName = "天合元创";//使用的签名
    private static final String TemplateCode = "SMS_210066581";//发送短信使用的模板

    private static IAcsClient acs;//服务对象
    private static SendSmsRequest req = new SendSmsRequest();//短信发送请求对象

    private static List<String> sentSms = new ArrayList<>();//发送过异常的联系人集合

    static {
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessKeySecret);
        acs = new DefaultAcsClient(profile);
    }

    @Resource
    private SysConfigService configService;

    /**
     * 方法描述：发送信息
     *
     * @param modelNum,sensorModel,sensorNum,warningValue
     */
    public synchronized void sendWarningInfo(List<WarningContacts> list, String modelNum, String sensorModel, String sensorNum, Double warningValue) {
        SysConfig one = configService.getOne(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getConfigKey, "jk.sendInfoIs"));
        if (ObjectUtil.isNotEmpty(one) && one.getConfigValue().equals("0")) {
            one.setConfigValue("1");
            configService.updateById(one);
            for (WarningContacts warningContacts : list) {
                sentSms.add(warningContacts.getContactsTel());
                WarningInfo warningInfo = new WarningInfo();
                warningInfo.setName(warningContacts.getContactsName());
                warningInfo.setTime(DateUtil.now());
                req.setPhoneNumbers(warningContacts.getContactsTel());//设置接收短信手机号
                req.setSignName(SignName);//设置使用签名
                req.setTemplateCode(TemplateCode);//设置使用通知模板id
                req.setTemplateParam(JSONUtil.toJsonStr(warningInfo));//设置请求参数 以json字符串形式与模板一致
                SendSmsResponse res = null;//向服务器发送请求
                try {
                    res = acs.getAcsResponse(req);
                } catch (ClientException e) {
                    e.printStackTrace();
                }
                if (res.getCode() == null && !res.getCode().equals("OK")) {
                    log.warn(res.getMessage());
                }
            }
        }
    }
}
